package itbour.onetouchshow.activity.myworks;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.widget.EditText;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.OneToucDialog;
import com.google.gson.Gson;
import com.lsjr.callback.EncryBeanCallBack;
import com.lsjr.utils.HttpUtils;

import java.util.HashMap;

import itbour.onetouchshow.App;
import itbour.onetouchshow.base.mvp.BasePresenterImpl;
import itbour.onetouchshow.bean.MyWorksBean;
import itbour.onetouchshow.bean.NoDataResult;
import itbour.onetouchshow.constants.AppConfig;
import itbour.onetouchshow.constants.AppConst;
import itbour.onetouchshow.utils.DeviceUtil;
import itbour.onetouchshow.utils.L_;
import itbour.onetouchshow.utils.T_;
import itbour.onetouchshow.utils.UIUtils;

/**
 * Created by guoh on 2018/7/10.
 * 功能描述：
 * 需要的参数：
 */
public class PersonalCreatePresenter extends BasePresenterImpl<MyworksContract.View> implements MyworksContract.Presenter {


    EditText passwordInput;

    public void loadMyWorksList(int mPageIndex) {
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("device", AppConst.ANDROID_DEVICE);
        stringObjectHashMap.put("userId", App.getUserId());
        //pageNum	是
        stringObjectHashMap.put("pageNum", mPageIndex);
        //页码 pageSize
        stringObjectHashMap.put("pageSize", AppConst.PAGE_SIZE);
        stringObjectHashMap.put("opTypes", "[4]");
        //   页容量
        stringObjectHashMap.put("withTotalPage", 1);
        HttpUtils.getInstance().executeGet(AppConfig.GET_USER_WORK_LIST_V3_0, stringObjectHashMap, new EncryBeanCallBack() {
            @Override
            protected void onXError(String exception) {
                T_.showCustomToast(exception);
                L_.e(exception);
                if (mvpView != null) {
                    mvpView.loadFaild(exception);
                }

            }

            @Override
            protected void onSuccess(String response) {
                NoDataResult noDataResult = new Gson().fromJson(response, NoDataResult.class);
                if (noDataResult == null || noDataResult.getList().size() == 0) {
                    mvpView.loadFaild(AppConst.NoData);
                } else {
                    if (mvpView != null) {
                        mvpView.loadSucceed(response);
                    }
                }
            }
        });
    }

    @SuppressLint("ValidFragment")
    public void deleteWork(Context mContext, MyWorksBean.ListBean listBean) {
        String context;
        //listBean.getWatermarkPay()
        if (listBean.getWatermarkPay() == 1) {
            context = "你已为该作品去除水印,删除后将无法恢复。";
        } else {
            context = "是否删除该作品?";
        }
        new OneToucDialog.Builder(mContext)
                .content(context)
                .positiveText("删除").onPositive(new OneToucDialog.SingleButtonCallback() {
            @Override
            public void onClick(OneToucDialog dialog, DialogAction which) {
                doRemoveWork(listBean);
            }
        })
                .negativeText("取消").onNegative(new OneToucDialog.SingleButtonCallback() {
            @Override
            public void onClick(OneToucDialog dialog, DialogAction which) {
                Log.e("TAH", "点击确定");
            }
        }).setType(OneToucDialog.Builder.ONE_TOUCH_TYPE)
                .cancelable(false)
                .show();
    }

    private void doRemoveWork(MyWorksBean.ListBean listBean) {
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("device", AppConst.ANDROID_DEVICE);
        stringObjectHashMap.put("userId", App.getUserId());
        stringObjectHashMap.put("docId", listBean.getId());
        HttpUtils.getInstance().executeGet(AppConfig.REMOVEUSERDOC_V1_0, stringObjectHashMap, new EncryBeanCallBack() {
            @Override
            protected void onXError(String exception) {
                L_.e(exception);
                T_.showCustomToast(exception);
            }

            @Override
            protected void onSuccess(String response) {
                if (mvpView != null)
                    mvpView.deleteSuccess(listBean);
            }
        });
    }

    public void reNameWork(Context context, MyWorksBean.ListBean listBean) {
        OneToucDialog build = new OneToucDialog.Builder(context)
                .positiveText("确定").onPositive(new OneToucDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(OneToucDialog dialog, DialogAction which) {
                        Log.e("TAH", "点击确定");
                        if (passwordInput.getText().length() == 0) {
                            return;
                        }
                        doReNameWork(listBean, passwordInput.getText().toString());
                    }
                })
                .negativeText("取消").onNegative(new OneToucDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(OneToucDialog dialog, DialogAction which) {
                        //  Log.e("TAH", "点击确定" + passwordInput.getText().toString());
                    }
                })
                .setType(OneToucDialog.Builder.ONE_TOUCH_TYPE)
                .cancelable(false).setShowEdittext(true).setmWidth(AppConst.APP_DIALOG_WIDTH).build();
        passwordInput = build.editText;
        passwordInput.setFilters(UIUtils.getInputFilter(12));
        build.show();
    }


    private void doReNameWork(MyWorksBean.ListBean listBean, String name) {
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("device", AppConst.ANDROID_DEVICE);
        stringObjectHashMap.put("userId", App.getUserId());
        L_.e(" listBean.getId()" + listBean.getId());
        stringObjectHashMap.put("docId", listBean.getId());
        stringObjectHashMap.put("name", name);
        HttpUtils.getInstance().executeGet(AppConfig.RENAMEUSERDOC_V1_0, stringObjectHashMap, new EncryBeanCallBack() {
            @Override
            protected void onXError(String exception) {
                L_.e(exception);
                T_.showCustomToast(exception);
            }

            @Override
            protected void onSuccess(String response) {
                listBean.setName(name);
                if (mvpView != null)
                    mvpView.reNameSuccess(listBean);
                L_.e(response);
            }
        });
    }


    /**
     * 作品预览  new
     */
    public void newLoadDocPreview(String docId) {
        L_.e("作品预览" + docId);
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("device", AppConst.ANDROID_DEVICE);
        stringObjectHashMap.put("userId", App.getUserId());
        stringObjectHashMap.put("docId", docId);
        HttpUtils.getInstance().executePost(AppConfig.REQUESTUSERDOCPRIVEWINFO_WEB_V1_1, stringObjectHashMap, new EncryBeanCallBack() {
            @Override
            protected void onXError(String exception) {
                T_.showCustomToast(exception);
                L_.e("newLoadDocPreview" + exception);
                if (mvpView != null) {
                    mvpView.loadFaild(exception);
                }
            }

            @Override
            protected void onSuccess(String response) {
                if (mvpView != null) {
                    L_.e("newLoadDocPreview" + response);
                    mvpView.loadNewDocPreviewSuccess(response);
                }
            }
        });
    }


    /**
     * 获取自由创建数据
     */
    public void getCustomVideodoccontent(String docId) {
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("device", AppConst.ANDROID_DEVICE);
        stringObjectHashMap.put("userId", App.getUserId());
        stringObjectHashMap.put("docId", docId);
        HttpUtils.getInstance().executeGet(AppConfig.CUSTOMVIDEODOCCONTENT, stringObjectHashMap, new EncryBeanCallBack() {
            @Override
            protected void onXError(String exception) {
                //T_.showToastReal(exception);
                if (mvpView != null) {
                    mvpView.loadFaild(exception);
                }
            }

            @Override
            protected void onSuccess(String response) {
                //T_.showToastReal(response);
                if (mvpView != null) {
                    mvpView.getCustomVideodoccontentResult(response);
                }

                L_.e(response);
            }
        });

    }

    /**
     * 获取自由创建配置项
     */

    public void getCustomVideoConfigure(String docId) {
        HashMap<String, Object> requestHashMap = new HashMap<>();
        requestHashMap.put("device", "620");
        requestHashMap.put("userId", "65");
        String appVersionName = DeviceUtil.getAppVersionName(UIUtils.getContext());
        requestHashMap.put("appVer", appVersionName);
        HttpUtils.getInstance().executeGet(AppConfig.GET_CUSTOM_VIDEO_CONFIGURE, requestHashMap, new EncryBeanCallBack() {
            @Override
            protected void onXError(String exception) {
                L_.i("response  CreatConfigInfo===" + exception);
                if (mvpView != null) {
                    mvpView.loadFaild(exception);
                }
            }

            @Override
            protected void onSuccess(String response) {
                L_.i("response  CreatConfigInfo===" + response);
                if (mvpView != null) {
                    mvpView.getCustomVideoConfigureResutl(response, docId);
                }
            }
        });

    }


}
